using System;
using System.Collections.Generic;
using System.Text;

using System.IO;

using System.Timers;

namespace LoggingServer
{
    public class LOGServerWriter
    {
        private static LOGServerWriter LogInstance = null;

        public static LOGServerWriter Instance
        {
            get
            {
                if (null == LogInstance)
                {
                    LogInstance = new LOGServerWriter();
                }

                return LogInstance;
            }
        }

        private Object LockObj = new Object();

        private Timer timer = new Timer();

        private StreamWriter sw = null;

        public LOGServerWriter()
        {
            Init();

            // check log every 30 secs
            timer.Interval = 30000;

            timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);

            timer.Start();
        }

        public void Init()
        {
            StaticComponent.LogStr("Init()");

            FileInfo fi = new FileInfo(StaticComponent.PathLogFile);

            if (!Directory.Exists(fi.DirectoryName))
            {
                StaticComponent.LogStr("Create Directory: " + fi.DirectoryName);
                Directory.CreateDirectory(fi.DirectoryName);
            }
        }

        void timer_Elapsed(object sender, ElapsedEventArgs e)
        {
            lock (LockObj)
            {
                try
                {
                    if (!File.Exists(StaticComponent.PathLogFile))
                    {
                        File.Create(StaticComponent.PathLogFile);
                    }

                    FileInfo fi = new FileInfo(StaticComponent.PathLogFile);

                    // file > 10mb
                    if (fi.Length >= 1024 * 1024 * 10)
                    {
                        sw.Close();
                        sw.Dispose();
                        sw = null;

                        fi.MoveTo(string.Format(@"{0}\{1:dMyyyy_HHmmss}.log", StaticComponent.PathLog, DateTime.Now));
                    }
                }
                catch (Exception ex)
                {
                    StaticComponent.LogEx(ex);
                }
            }
        }

        private void WriteLog(string str)
        {
            try
            {
                if (null == sw)
                {

                    if (!File.Exists(StaticComponent.PathLogFile))
                    {
                        FileStream fs = new FileStream(StaticComponent.PathLogFile, FileMode.CreateNew, FileAccess.ReadWrite, FileShare.ReadWrite); //File.Create(StaticComponent.PathLogFile);
                        sw = new StreamWriter(fs);
                    }
                    else
                    {
                        sw = new StreamWriter(new FileStream(StaticComponent.PathLogFile, FileMode.Append));
                    } 
                }

                sw.WriteLine(str);
                sw.Flush();
            }
            catch (Exception ex)
            {
                StaticComponent.LogEx(ex);
                sw = null;
            }
        }

        public void WriteLine(string logStr)
        {
            lock (LockObj)
            {
                try
                {
                    if (!string.IsNullOrEmpty(logStr))
                    {
                        WriteLog(logStr);
                        Console.WriteLine(logStr);
                    }
                }
                catch (Exception ex)
                {
                    StaticComponent.LogEx(ex);
                }
            }
        }
    }
}
